package com.yanqu.road.server.cmd.activity.suoyao;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.suoyao.SuoYaoUserData;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.suoyao.SuoYaoModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suoyao.SuoYaoMgr;
import com.yanqu.road.server.pb.SuoYaoPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SUOYAO_ACTIVITY_GET_USER_DATA, desc = "锁妖塔获取用户数据")
public class GetSuoYaoUserDataCmd extends AbstractCommand {

    private SuoYaoActivityProto.SuoYaoUserDataMsg.Builder userDataMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = SuoYaoMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_OPEN;
            return;
        }
        SuoYaoUserData userData = player.getModule(SuoYaoModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_UNLOCK;
            return;
        }

        userDataMsg = SuoYaoPb.parseSuoYaoUserDataMsg(userData);
    }

    @Override
    public void sendData(GamePlayer player) {
        SuoYaoActivityProto.GetSuoYaoUserDataRespMsg.Builder builder = SuoYaoActivityProto.GetSuoYaoUserDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setUserData(userDataMsg);
        }
        player.sendPacket(ClientProtocol.U_SUOYAO_ACTIVITY_GET_USER_DATA, builder);
    }
}
